//
//  KDTypeBtnBoard.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/6/13.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDTypeBtnBoard.h"
#import "KDTypeBtnBoardModel.h"
#import "Masonry.h"
#import "RightPageBtn.h"
CGFloat const MAGIN = 5;
/** 一行最多放几个*/
NSInteger const MAXCOUNT_BTN = 4;
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;
@interface KDTypeBtnBoard ()
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) RightPageBtn *selectBtn;
/***  底部的scrollView */
@property (nonatomic, weak) UIScrollView *scrollView;

@end
@implementation KDTypeBtnBoard

+ (instancetype)typeBtnBoardWithFrame:(CGRect)frame
{
    return [[self alloc]initWithFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
    }
    self.userInteractionEnabled = YES;
    return self;
}
- (void)setSelectBtn:(RightPageBtn *)selectBtn
{
    _selectBtn.selected = NO;
    _selectBtn = selectBtn;
    selectBtn.selected = YES;
}
- (void)setModel:(KDTypeBtnBoardModel *)model
{
    _model = model;
    if (!model) {
        return;
    }
    [self setUptypeBtnBoard];
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
//    [self setUptypeBtnBoard];
}

- (void)setUpScrollView{
    //加一个挡板
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor clearColor];
    [self addSubview:view];
    //初始化underScrollView
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    self.scrollView = scrollView;
    CGFloat labelMaxY = CGRectGetMaxY(self.label.frame);
    scrollView.frame = CGRectMake(0, labelMaxY, self.width, self.height - labelMaxY);
    
    //    underScrollView.contentSize = CGSizeMake(BTN_WIDTH * PAGE_NUMBER, HEADSCROLLH);
    scrollView.backgroundColor = RandomColor;
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = YES;
    [self addSubview:self.scrollView];

}
- (void)setUptypeBtnBoard
{
    for (UIView *subView in self.subviews) {
//        if ([subView isKindOfClass:[RightPageBtn class]]) {
            [subView removeFromSuperview];
//        }
    }
    //添加分隔线
    UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100-1, self.width, 1)];
//    iv.image = [UIImage imageNamed:@"分隔线"];
    iv.backgroundColor = WQColor(221, 221, 221);
    [self addSubview:iv];
//    添加label
//    if (!self.label) {
        UILabel *label = [[UILabel alloc]init];
        self.label = label;
        label.text = self.model.labelText;
//        label.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:18];
        label.textColor = WQColor(50, 50, 50);
        [self addSubview:label];
        label.frame = CGRectMake(26 , 15, 100, 20);
        KDLog(@"%@",NSStringFromCGRect(label.frame));

//    }
    [self setUpScrollView];
    KDLog(@"%@",NSStringFromCGRect(self.scrollView.frame));
    //添加btn
    NSInteger count = self.model.btnTitleArr.count;
    CGFloat X = self.label.x;//将要创建的btn的X（同时也是上一个btn的maxX＋Magin）
    CGFloat Y = MAGIN; // //将要创建的btn的Y
    for (int i = 0; i < count; i++) {
        NSString *title = self.model.btnTitleArr[i];
        CGRect labelRect = [title boundingRectWithSize:CGSizeMake(screenW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
        RightPageBtn *btn = [RightPageBtn buttonWithType:UIButtonTypeCustom];
        CGFloat height = 30;
        CGFloat width = labelRect.size.width + 20;
        if (width + X  <= self.width) {//说明不用换行
            btn.frame = CGRectMake( X, Y, width, height);
        }else{//说明需要换行
            btn.frame = CGRectMake( self.label.x, Y + height + MAGIN, width, height);
        }
        [btn setTitle:title forState:UIControlStateNormal];
        btn.tag = [self.model.btnTagArr[i] integerValue];
        if (!self.selectBtn && btn.tag == 10) {//如果没有被选中的Btn，那么就默认被选中的btn是“全部”按钮
            self.selectBtn = btn;
        }
        if (btn.tag == self.model.selectBtnTag) {
            self.selectBtn = btn;
        }
        btn.backgroundColor = [UIColor whiteColor];
        //如果是一行，就高度居中一点；如果是两行，就不那么干。
        btn.centerY = (count <= MAXCOUNT_BTN)? self.scrollView.height * 0.5: btn.centerY;
        btn.enabled = YES;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        X = CGRectGetMaxX(btn.frame) + MAGIN;
        Y = btn.y;
        [self.scrollView addSubview:btn];
        if (i == count-1) {
            self.scrollView.contentSize = CGSizeMake(1, CGRectGetMaxY(btn.frame) +10);
        }
    }
    
}

- (void)btnClick:(RightPageBtn *)btn
{
    self.selectBtn = btn;
    if (_delegate && [_delegate respondsToSelector:@selector(typeBtnBoard:BtnClickedWithTag:andLabelTitle:)]) {
        [_delegate typeBtnBoard:self BtnClickedWithTag:btn.tag andLabelTitle:self.label.text];
    }
}
@end
